/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * panel.java
 *
 * Created on 29-jun-2011, 16:18:58
 */

package adminasistencia;

import com.mysql.jdbc.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author micael
 */
public class panel extends javax.swing.JFrame {
    private PreparedStatement consultarRegistro;
   public int run;


    /** Creates new form panel */
    public panel() {
        
    }

  

   public panel(int run) {
       initComponents();
       setLocationRelativeTo(null);//centrar la pantalla
       this.run = run;
        nombreTxt.setText(run+"");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        txtFecha1 = new datechooser.beans.DateChooserCombo();
        jButton2 = new javax.swing.JButton();
        txtFecha2 = new datechooser.beans.DateChooserCombo();
        jScrollPane1 = new javax.swing.JScrollPane();
        taLista = new javax.swing.JTable();
        nombreTxt = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(600, 600));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("USUARIO");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 20, -1, -1));

        jButton1.setText("SALIR");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 450, 170, 50));

        txtFecha1.setCurrentView(new datechooser.view.appearance.AppearancesList("custom",
            new datechooser.view.appearance.ViewAppearance("custom",
                new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("DejaVu Sans", java.awt.Font.PLAIN, 13),
                    new java.awt.Color(0, 0, 0),
                    new java.awt.Color(0, 0, 255),
                    false,
                    true,
                    new datechooser.view.appearance.swing.ButtonPainter()),
                new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("DejaVu Sans", java.awt.Font.PLAIN, 13),
                    new java.awt.Color(0, 0, 0),
                    new java.awt.Color(0, 0, 255),
                    true,
                    true,
                    new datechooser.view.appearance.swing.ButtonPainter()),
                new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("DejaVu Sans", java.awt.Font.PLAIN, 13),
                    new java.awt.Color(0, 0, 255),
                    new java.awt.Color(0, 0, 255),
                    false,
                    true,
                    new datechooser.view.appearance.swing.ButtonPainter()),
                new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("DejaVu Sans", java.awt.Font.PLAIN, 13),
                    new java.awt.Color(128, 128, 128),
                    new java.awt.Color(0, 0, 255),
                    false,
                    true,
                    new datechooser.view.appearance.swing.LabelPainter()),
                new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("DejaVu Sans", java.awt.Font.PLAIN, 13),
                    new java.awt.Color(0, 0, 0),
                    new java.awt.Color(0, 0, 255),
                    false,
                    true,
                    new datechooser.view.appearance.swing.LabelPainter()),
                new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("DejaVu Sans", java.awt.Font.PLAIN, 13),
                    new java.awt.Color(0, 0, 0),
                    new java.awt.Color(255, 0, 0),
                    false,
                    false,
                    new datechooser.view.appearance.swing.ButtonPainter()),
                (datechooser.view.BackRenderer)null,
                false,
                true)));
    txtFecha1.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.LOWERED,
        (java.awt.Color)null,
        (java.awt.Color)null));
getContentPane().add(txtFecha1, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 50, -1, -1));

jButton2.setText("BUSCAR");
jButton2.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton2ActionPerformed(evt);
    }
    });
    getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 90, 130, -1));

    txtFecha2.setCurrentView(new datechooser.view.appearance.AppearancesList("custom",
        new datechooser.view.appearance.ViewAppearance("custom",
            new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("DejaVu Sans", java.awt.Font.PLAIN, 13),
                new java.awt.Color(0, 0, 0),
                new java.awt.Color(0, 0, 255),
                false,
                true,
                new datechooser.view.appearance.swing.ButtonPainter()),
            new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("DejaVu Sans", java.awt.Font.PLAIN, 13),
                new java.awt.Color(0, 0, 0),
                new java.awt.Color(0, 0, 255),
                true,
                true,
                new datechooser.view.appearance.swing.ButtonPainter()),
            new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("DejaVu Sans", java.awt.Font.PLAIN, 13),
                new java.awt.Color(0, 0, 255),
                new java.awt.Color(0, 0, 255),
                false,
                true,
                new datechooser.view.appearance.swing.ButtonPainter()),
            new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("DejaVu Sans", java.awt.Font.PLAIN, 13),
                new java.awt.Color(128, 128, 128),
                new java.awt.Color(0, 0, 255),
                false,
                true,
                new datechooser.view.appearance.swing.LabelPainter()),
            new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("DejaVu Sans", java.awt.Font.PLAIN, 13),
                new java.awt.Color(0, 0, 0),
                new java.awt.Color(0, 0, 255),
                false,
                true,
                new datechooser.view.appearance.swing.LabelPainter()),
            new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("DejaVu Sans", java.awt.Font.PLAIN, 13),
                new java.awt.Color(0, 0, 0),
                new java.awt.Color(255, 0, 0),
                false,
                false,
                new datechooser.view.appearance.swing.ButtonPainter()),
            (datechooser.view.BackRenderer)null,
            false,
            true)));
getContentPane().add(txtFecha2, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 50, -1, -1));

taLista.setModel(new javax.swing.table.DefaultTableModel(
    new Object [][] {

    },
    new String [] {

    }
    ));
    jScrollPane1.setViewportView(taLista);

    getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 140, -1, 290));

    nombreTxt.setText("jLabel2");
    getContentPane().add(nombreTxt, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 20, -1, -1));

    pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    System.exit(0);        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
     String recibeDia1, recibeMes1, recibeAno1, fecha1, recibeDia2, recibeMes2, recibeAno2, fecha2;
     


        //El siguiente if es para dar el formato a la fecha
        if(txtFecha1.getText().length()==7){//Si el largo de la fecha es 7 se da el siguinete formato
        recibeDia1=txtFecha1.getText().substring(0,1);// esto se da porque el dia tiene un largo de 2
        recibeMes1 = txtFecha1.getText().substring(2,4);
        recibeAno1 = txtFecha1.getText().substring(5,7);
        fecha1= "20" + recibeAno1 + "-" + recibeMes1 + "-0" + recibeDia1;

        }else{
            recibeDia1 = txtFecha1.getText().substring(0,2);
            recibeMes1 = txtFecha1.getText().substring(3,5);
            recibeAno1 = txtFecha1.getText().substring(6,8);
            fecha1= "20" + recibeAno1 + "-" + recibeMes1 + "-" + recibeDia1;

        }

      //El siguiente if es para dar el formato a la fecha
        if(txtFecha2.getText().length()==7){//Si el largo de la fecha es 7 se da el siguinete formato
        recibeDia2=txtFecha2.getText().substring(0,1);// esto se da porque el dia tiene un largo de 2
        recibeMes2 = txtFecha2.getText().substring(2,4);
        recibeAno2 = txtFecha2.getText().substring(5,7);
        fecha2= "20" + recibeAno2 + "-" + recibeMes2 + "-0" + recibeDia2;

        }else{
            recibeDia2 = txtFecha2.getText().substring(0,2);
            recibeMes2 = txtFecha2.getText().substring(3,5);
            recibeAno2 = txtFecha2.getText().substring(6,8);
            fecha2= "20" + recibeAno2 + "-" + recibeMes2 + "-" + recibeDia2;

        }
     //JOptionPane.showMessageDialog(rootPane, "RUN"+run+"Fecha Inicio "+fecha1+" - Fecha Final "+fecha2 );


     consultas conectar = new consultas();
     conectar.initDB();
     ResultSet existe=null;
        try {
            consultarRegistro = (PreparedStatement) conectar.dbConnection.prepareStatement("SELECT * FROM asistencia WHERE run = "+run+" and DATE(Fecha_hora_Entrada) BETWEEN '"+ fecha1 +"' and '"+fecha2+"'");
        } catch (SQLException ex) {
            Logger.getLogger(panel.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            existe = consultarRegistro.executeQuery();
        } catch (SQLException ex) {
            Logger.getLogger(panel.class.getName()).log(Level.SEVERE, null, ex);
        }


        //jScrollPane1 = new JScrollPane();
        DefaultTableModel modelo = new DefaultTableModel();
         modelo.addColumn("Nº");
         modelo.addColumn("Fecha/Hora Entrada");
         modelo.addColumn("Fecha/Hora Salida ");
        try {
            while (existe.next()) {
                Object[] fila = new Object[3];
               
                    fila[0] = existe.getObject(1);
                    fila[1] = existe.getObject(3);
                      if (existe.getString("Estado").equals("P"))
                    {
                    fila[2] = "Presente";
                    System.out.println("Presente");
                    }
                    else
                    {
                        System.out.println("NOOOO ");
                    fila[2] = existe.getObject(4);// El primer indice en rs es el 1, no el cero, por eso se suma 1.
                    }
                
                /* PARA VALIDAR LOS DIAS TRANSCURRIDOS
                //    SE DEBE CREAR UN IF COMPARANDO MICONEXION.RS.GETOBJECT(2) QUE SERIA LA FECHA DE TERMINO
                CON LA FUNCION CREADA PARA CALCULAR*/
                modelo.addRow(fila);
            }
        } catch (SQLException ex) {
            Logger.getLogger(panel.class.getName()).log(Level.SEVERE, null, ex);
        }
        taLista.setModel(modelo);

        //miConexion.desconectar();
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new panel().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel nombreTxt;
    private javax.swing.JTable taLista;
    private datechooser.beans.DateChooserCombo txtFecha1;
    private datechooser.beans.DateChooserCombo txtFecha2;
    // End of variables declaration//GEN-END:variables

}
